package com.msmall.community.common.interceptor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.pager.PaginationSupport;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class HTMLPaginationInterceptor implements Interceptor{

	private static final long serialVersionUID = -7667450084229247477L;
	
	private final Log log = LogFactory.getLog(HTMLPaginationInterceptor.class);
	
	public String intercept(ActionInvocation invocation) throws Exception {
		Object action = invocation.getAction();
		if (action instanceof HTMLBaseAction) {
			HTMLBaseAction hba = (HTMLBaseAction) action;
			PaginationSupport page = hba.getPage();
			int pageIndex = 1;
			int pageSize = 20;
			String pageInfo = hba.getPi();
			if(pageInfo != null && pageInfo.length() >0){
				try{
					String[] pi = pageInfo.split("_");
					pageIndex = Integer.valueOf(pi[0]);
					if(pi.length ==2)
						pageSize = Integer.valueOf(pi[1]);
				}catch(Exception e){}
			}
			if(page == null)
				page = new PaginationSupport();
			page.setPageIndex(pageIndex);
			page.setPageSize(pageSize);
			hba.setPage(page);
			hba.setPi(page.getPageIndex() + "_" + page.getPageSize());
		}
		return invocation.invoke();
	}
	
	public void destroy() {
	}

	public void init() {
	}



}
